package com.zhiyou100.springbootreidsquartz.scheduler;

import com.zhiyou100.springbootreidsquartz.job.RedisJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

/**
 * 执行器
 */
public class RedisTrigger {

    public static void main(String[] args) throws SchedulerException, InterruptedException {
        //1创建调度器
        StdSchedulerFactory factory =  new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        //2创建jobDetail实例,跟(MyJob绑定)(我得实现)
        JobDetail jobDetail = JobBuilder.newJob(RedisJob.class).withIdentity("job1", "group1").build();
        //3创建
        SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggergroup1")
                .startNow()//立刻生效
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().
                        withIntervalInMinutes(2)//每过1秒执行一次(可以修改方法来修改时间)
                        .repeatForever())//一直执行
                .build();
        //执行调度器
        scheduler.scheduleJob(jobDetail,simpleTrigger);
        scheduler.start();

        //一直启动
        /*//关闭
        TimeUnit.MINUTES.sleep(1);
        scheduler.shutdown();*/

    }
}
